//
//  BaseTabBarController.m
//  XiaoHongShu
//
//  Created by Jason on 2022/6/15.
//

#import "BaseTabBarController.h"
#import "MainViewController.h"
#import "ShopViewController.h"
#import "PublishViewController.h"
#import "MessageViewController.h"
#import "MeViewController.h"
#import "BaseNavigationController.h"

@interface BaseTabBarController ()<UITabBarControllerDelegate, UITabBarDelegate>

@end

@implementation BaseTabBarController

+(void)initialize {
    NSDictionary *attrNormal = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:15],NSForegroundColorAttributeName:kLightGrayColor};
    NSDictionary *attrSelect = [NSDictionary dictionary];
    UITabBar *tabBar = [UITabBar appearance];
    //ios 13 之后需要这样设置才有效
    if (@available(iOS 13.0, *)) {
        attrSelect = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:15],NSForegroundColorAttributeName:[UIColor labelColor]};
        UITabBarAppearance *tabBarAppearance = [[UITabBarAppearance alloc]init];
        //设置tabar背景色
        tabBarAppearance.backgroundColor = [UIColor secondarySystemGroupedBackgroundColor];
        tabBarAppearance.stackedLayoutAppearance.normal.titleTextAttributes = attrNormal;
        tabBarAppearance.stackedLayoutAppearance.selected.titleTextAttributes = attrSelect;
        //必须要加上这两句
        tabBar.standardAppearance = tabBarAppearance;
        if (@available(iOS 15.0, *)) {
            tabBar.scrollEdgeAppearance = tabBarAppearance;
        } else {
            // Fallback on earlier versions
        }
    } else {
        attrSelect = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:15],NSForegroundColorAttributeName:kBlackColor};
        UITabBarItem *tbItem = [UITabBarItem appearance];
        [tbItem setTitleTextAttributes:attrNormal forState:UIControlStateNormal];
        [tbItem setTitleTextAttributes:attrSelect forState:UIControlStateSelected];
        [tabBar setBarTintColor:kWhiteColor];  //tabBar的背景色
    }
    tabBar.translucent = YES;     //translucent: 半透明的
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.delegate = self;
    
    [self allChildView];
}

- (void) allChildView
{
    [self addChildViewController:[MainViewController new] tabBarItemTitle:@"首页" itemImgName:@"login_slices" itemSelectedImgName:@""];
    [self addChildViewController:[ShopViewController new] tabBarItemTitle:@"购物" itemImgName:@"" itemSelectedImgName:@""];
    [self addChildViewController:[PublishViewController new] tabBarItemTitle:@"" itemImgName:@"administer_icon_invite" itemSelectedImgName:@"administer_icon_invite"];
    [self addChildViewController:[MessageViewController new] tabBarItemTitle:@"消息" itemImgName:@"" itemSelectedImgName:@""];
    [self addChildViewController:[MeViewController new] tabBarItemTitle:@"我" itemImgName:@"" itemSelectedImgName:@""];
}

-(void)addChildViewController:(UIViewController *)viewController tabBarItemTitle:(NSString *)title itemImgName:(NSString *)itemImgName itemSelectedImgName:(NSString *)itemSelectedImgName {
    
    UIImage *normalImage = [UIImage imageNamed:itemImgName];
    UIImage *selectedImage = [UIImage imageNamed:itemSelectedImgName];
    
    UITabBarItem * messageItem = [[UITabBarItem alloc]initWithTitle:title image:[normalImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]selectedImage:[selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    viewController.tabBarItem = messageItem;
    BaseNavigationController *navMessage = [[BaseNavigationController alloc]initWithRootViewController:viewController];
    
    viewController.title = title;
    [self addChildViewController:navMessage];
    
    if (title == nil || title.length == 0) {
        NSLog(@"tag = %ld",messageItem.tag);
        messageItem.imageInsets = UIEdgeInsetsMake(15, 0, -15, 0);
    }
}

#pragma  mark - UITabBarControllerDelegate
//- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController API_AVAILABLE(ios(3.0)) {
//    NSLog(@"--shouldSelectViewController--%@",viewController);
//    return  YES;
//}

//点击tabBarItem的时候触发
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    NSLog(@"didSelectViewController- %@",viewController);
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
